//
//  BCIndustryScreeningCoinTypeView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/19.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCIndustryScreeningCoinTypeView.h"

//layout
#import "UICollectionViewLeftAlignedLayout.h"
//cell
#import "BCIndustryScreeningDisplayCell.h"
//
#import "BCUserWalletCurrDataModel.h"

@interface BCIndustryScreeningCoinTypeView () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateLeftAlignedLayout>

//@property (nonatomic, strong) UICollectionView *typeCollection;

@end

@implementation BCIndustryScreeningCoinTypeView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
        
    }
    return self;
}
- (void)setupSubviews {
    self.customTitleLabel.text = nil;
    //
    UICollectionViewLeftAlignedLayout *flowLayout = [UICollectionViewLeftAlignedLayout new];
    self.typeCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(BCIndustryScreeningBaseLeftPadding, 0, ScreenWidth-2*BCIndustryScreeningBaseLeftPadding, 0) collectionViewLayout:flowLayout];
    self.typeCollection.dataSource = self;
    self.typeCollection.delegate = self;
    self.typeCollection.backgroundColor = [UIColor clearColor];
    self.typeCollection.scrollEnabled = false;
    [self addSubview:self.typeCollection];
    [self.typeCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customTitleLabel.mas_bottom);
        make.centerX.mas_equalTo(self);
        make.width.mas_equalTo(self).offset(-2*BCIndustryScreeningBaseLeftPadding);
        make.bottom.mas_equalTo(self);
    }];
    //
    [self.typeCollection registerClass:BCIndustryScreeningDisplayCell.class forCellWithReuseIdentifier:NSStringFromClass(BCIndustryScreeningDisplayCell.class)];
}

- (void)setSource:(NSArray<BCUserWalletCurrDataModel *> *)source {
    _source = source;
    //
    [self.typeCollection reloadData];
}


#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateLeftAlignedLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.source.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BCUserWalletCurrDataModel *model = self.source[indexPath.row];
    BCIndustryScreeningDisplayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(BCIndustryScreeningDisplayCell.class) forIndexPath:indexPath];
    cell.displayContent = model.acronyms;
    cell.selected = model.isSelect;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = floorf((collectionView.frame.size.width-3*10.0)/4);
    CGFloat height = 32.0f;
    return CGSizeMake(width, height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsZero;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10.0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //如果已经选择取消选中
    BCUserWalletCurrDataModel *model = self.source[indexPath.row];
    if (self.currModel==model) {
        model.isSelect = false;
        self.currModel = nil;
    }else {
        self.currModel.isSelect = false;
        model.isSelect = true;
        self.currModel = model;
    }
    [collectionView reloadData];
}



- (CGFloat)viewHeight {
    CGFloat height = [super viewHeight];
    height += self.typeCollection.collectionViewLayout.collectionViewContentSize.height;
    return height;
}

@end
